﻿// MIT License
//
// Copyright (c) 2021-present zuohuaijun, Daming Co.,Ltd and Contributors
//
// 电话/微信：18020030720 QQ群1：87333204 QQ群2：252381476

using Microsoft.Extensions.Hosting;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Numeric;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinDraftBatchGetResponse.Types.Item.Types;

namespace Admin.Net.InformationPlatform.Service.Inventory;
public class InventoryHeartBeatService : IHostedService
{
    DateTime oneOClock = DateTime.Today.AddHours(8);

    private Timer _timer;



    public Task StartAsync(CancellationToken cancellationToken)
    {
        // _timer = new Timer(SendSms, null, TimeSpan.Zero, TimeSpan.FromHours(1));
        SendSms();
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
      //  _timer?.Dispose();
        return Task.CompletedTask;
    }


    private void SendSms() //object state
    {
       
        
          var task1 = new Task(() =>
          {
              while (true)
              {
                  DateTime now = DateTime.Now;
                  if (now > oneOClock)
                  {
                      oneOClock = oneOClock.AddDays(1.0);
                      TaskTime();

                  }

              }


          });

          task1.Start();
    }




    private void TaskTime() {


        string homeUrl = "http://localhost:5009/Inventory/SendSms";
        string serverUrl = "http://10.8.1.151:8096/Inventory/SendSms";
        using (HttpClient client = new HttpClient())
        {

            var content = new StringContent("", Encoding.UTF8, "application/json");
            //执行任务
            var response = client.PostAsync(serverUrl, content).Result;


            // 检查响应状态码
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Heartbeat sent successfully");
            }
            else
            {
                Console.WriteLine("Failed to send heartbeat");
            }

        }
    }



}